* Note that this analysis requires structural change on the build data

		gl pos_script_modified ///
		${FilePath2}/build/alternative/build_2_data_cleaning_sub_post_modified.do
		
* (2) Prepare the data set for analysis ----------------------------------------

	* Load the data
	use $output1, clear
	
	* Switch to the appropriate dates
	gen pre 				= pre_${spec}
	gen post 				= post_${spec}
	gen training 		 	= training_${spec}
	gen training_started 	= ${spec}_training_started
	gen training_ended	 	= ${spec}_training_ended
	
	* Sample restriction: 
	* Limit the lower and upper bound, and [remove data during training for treatment]
	* [] indicates diviations from our traditional regression where that data is remove for both C and T
	
	keep if inrange(year_month, $prestart, $postend) & ///
			((treatment ==1 & (training==0)) | ///
			(treatment ==0))
	
	* List of variables to be included in PRS calculations
	gl prs_items 	warrant veh_rec firearm traf_stop driv_stop cont anov ///
					haz_cit nonhaz_cit curf_vio cta park_cit isrs
					
					// Warrant, vehicle recover, firearm violation 
					// traffic stop, driver stop, ANOVs, 
					// Citations hazard, citation non-hazard, curfew violation
					// parking citation, ISRs
	
* (3) Varable creations : PRE SITD ---------------------------------------------

	do $pre_script
	
			
* (4) Stack the data -----------------------------------------------------------
	
	* IMPORTANT: Starting from this section, the data only contain post-training data
	replace post = 1 if (inrange(year_month, 202004, 202202) & treatment == 0)
	keep if (post == 1)
	
	* Identify month post training
	*replace training_ended = 202003 if treatment == 0
	gen year_dif = floor(year_month/100) - floor(training_ended / 100)
	gen month = (year_month - training_ended) + (year_dif * (12 - 100))

		* Cloak traing endate for all control officers
		****
		replace month = 1 if treatment == 0										//
			
	* Control officer data : Note that this only apply for months 1-4
	gen data_month = .
	
	foreach t in 202003 202009 202010 202011 202012 202101 202102 {
		
		* Build the block of months from treatment group
		****
		qui: levelsof year_month if ///
		(training_ended == `t') & (treatment == 1) & inrange(month, 1, 4), ///
		local(all_month)
		local range ""
		foreach m in `all_month' {
			local range "`range', `m'"
		}
		replace data_month = .
		replace data_month = 1 if inlist(year_month `range')
		
		* Loop through each block available
		levelsof strata if ///
		(treatment == 1) & (training_ended == `t') & (data_month == 1), ///
		local(levels)	
		
			* Loop through each strata: keepping only control officers
			foreach l in `levels' {
				preserve
				keep if (data_month == 1) & (strata == `l') & (treatment == 0)
				count
				tempfile s`l'
				save `s`l''
				restore
			}
			
			* Combine and save
			preserve
				clear
				foreach l in `levels' {
				append using `s`l''
				}
				
				gen cohort = `t'
				tempfile s_`t'
				save `s_`t''
			restore

		
	}
	
	* Now combine with treatment officer data
	keep if (treatment == 1)
	gen cohort = training_ended
	foreach t in 202003 202009 202010 202011 202012 202101 202102 {
	app using `s_`t''
	}

	* Deduplication: This is the core difference between the stack and non stack
	bys employee_id year_month: keep if _n == 1
	

* (4) Variable creations : POST SITD -------------------------------------------
	
	* Specify post script
	gl alternative_spec 1_4
	
	* Call the subscript
	do $pos_script
	
			
	* (5) Final data cleaning --------------------------------------------------

		* Individual identifier
		bys employee_id: gen i = _n == 1
	
	* (6) Analysis -non-stacked ----------------------------------------------------

		* Change in cohort
		replace cohort = strata *1000000 + cohort
		
		* Rename
		gl oldnames l12_trr_C trr_nf_p2020_1_C trr_nf_p2020_2_C ///
					arrest_con_cop ///
					days_iod ///
					l23_trr_C trr_C ///
					l12_trr_s_i_C l12_trr_sa_injury_C l12_trr_s_h_C l12_trr_s_hos_i_ix_C ///
					ttt_score_cut_Am  ///
					award total_w_exo_accusations ///
					prs_score_w_arrs_cut_Am 		
		gl newnames NLU UL1 UL2 ///
					DAR ///
					OI ///
					LTU AU ///
					NLUSI NLUSA NLUH NLUSH ///
					TTT ///
					AW COM ///
					AI 
		* Rename
		rename ($oldnames) ($newnames)										
				
		mat results = J(1, 5,.)
		foreach type in arr_a arr_d arr_nd  {
		foreach race in all black nblack white hisp other {
		
			
			foreach period in $alternative_spec {									
			local outcome `type'_`race'
				
			* Coding values for missing consistency
			replace `outcome' = 0 if (`outcome'==. & missing ==0)
			replace `outcome' = . if (missing ==1)
		
			* CM and ITT
			qui: sum `outcome' if (treatment == 0) & (months_`period' == 1)
			local CM = r(mean)
			
			qui: reg `outcome' treatment ${picked5a} i.strata i.year_month ///
			if (months_`period' == 1), vce(cluster employee_id)
			qui: test treatment
			
			mat add = `CM', _b[treatment], _se[treatment], r(p),e(N)
			mat rownames add = `outcome'_`period'
			mat results = results \ add
			mat `type'_`race'_m_`period' = add
			
		}
		}
		}
		
		* Note on i.cohort
		mat li results
		
		* Regression for other outcomes --------------------------------------------
		foreach outcome in NLU DAR OI AI {
		
		qui: sum `outcome' if (treatment == 0)
		qui: local CM = r(mean)
		
		qui: reg `outcome' treatment ///
		${picked5a} i.strata i.year_month if (months_${alternative_spec} == 1 | (treatment==0) ), vce(cluster employee_id)
		qui: test treatment
																					//
		mat `outcome' =  `CM', _b[treatment], _se[treatment], r(p), e(N)
		
		}
		
		mat all_${alternative_spec} = NLU \ DAR \ OI \ AI
		mat li all_${alternative_spec}
		
		gl family NLU DAR
		PQ
		
		gl family OI AI 
		PQ
		
		mat Q_${alternative_spec} = q_NLU \ q_DAR \ q_OI \ q_AI
		mat li Q_${alternative_spec}
		
		foreach m in NLU DAR OI AI {
			mat `m'_months_1_4_t = q_`m'
		}
		
		
		* Table B18
		gl month_segments months_1_4_t 
		table_start_horizontal ///
		${report_folder}/Admin_key_outcomes_alternative_spec.txt ///
		"Alternate Allocation of Control Officers to Post-training Periods" 130 "one_p"
		midrule

			write_horizontal 	NLU	1000		"Uses of non-lethal force" 		CmCoSePvQv
			write_horizontal 	DAR	1000		"Discretionary arrests" 		CmCoSePvQv	
			write_horizontal 	OI	1			"Officer injuries (days off)" 	CmCoSePvQv
			write_horizontal 	AI	1			"Officer activities (index)" 	RcCoSePvQv

		local N : di %9.0fc NLU_months_1_4_t[1,6]
		write_end end_alternative_spec `N'
		
		table_end "$end_alternative_spec" Admin_key_outcomes_alternative_spec